package controllers

import (
	"Godzilla/common/request"
	"Godzilla/common/responses"
	"Godzilla/common/validate"
	"Godzilla/enum/code"
	"Godzilla/enum/fileds"
	"Godzilla/models"
	"Godzilla/repositories"
	"github.com/labstack/echo/v4"
	"net/http"
	"strconv"
	"unicode/utf8"
)

func SaveRole(c echo.Context) error {
	var (
		name string
	)

	u := new(models.Role)
	if err := c.Bind(u); err != nil {
		return c.JSON(http.StatusOK, responses.Response(code.ParameterError, "绑定参数有误", err))
	}

	name = u.Name

	if name == "" {
		return c.JSON(http.StatusOK, responses.Response(code.ParameterError, "角色名称不能为空", name))
	}

	if utf8.RuneCountInString(name) <= 1 || utf8.RuneCountInString(name) > 10 {
		return c.JSON(http.StatusOK, responses.Response(code.ParameterError, "角色不能小于2大于11个字符", ""))
	}

	roleManager, err := repositories.NewRoleManager()
	if err != nil {
		c.JSON(http.StatusOK, responses.Response(code.QueryError, "查询有误", err.Error()))
	}
	id, err := roleManager.InsertRole(name)
	if err != nil {
		return c.JSON(http.StatusOK, responses.Response(code.QueryError, "sql错误", err.Error()))
	}

	return c.JSON(http.StatusOK, responses.Response(200, "成功", id))
}


func GetRole(c echo.Context) error {
	var (
		id  string
	)

	id = c.Param("id")
	if id == "" {
		c.JSON(http.StatusOK, responses.Response(code.ParameterError, "id不能为空", ""))
	}




	roleManager, err := repositories.NewRoleManager()
	if err != nil {
		c.JSON(http.StatusOK, responses.Response(code.QueryError, "查询有误", err.Error()))
	}
	role, error := roleManager.SelectRoleById(id)
	if error != nil {
		c.JSON(http.StatusOK, responses.Response(code.QueryError, "查询有误", error.Error()))
	}
	return c.JSON(http.StatusOK, responses.Response(200, "成功", role))
}

func UpdateRole(c echo.Context) error {
	var (
		getId   string
		id		int64
		name	string
		err		error
	)

	getId = c.Param("id")
	if getId == "" {
		return c.JSON(http.StatusOK, responses.Response(code.ParameterError, "id不能为空", ""))
	}
	id, err = strconv.ParseInt(getId, 10, 64)
	if err != nil {
		c.JSON(http.StatusOK, responses.Response(code.ParameterError, err.Error(), ""))
	}


	r := new(models.Role)
	if err := c.Bind(r); err != nil {
		return c.JSON(http.StatusOK, responses.Response(code.ParameterError, "绑定参数失败", err))
	}
	name = r.Name

	if utf8.RuneCountInString(name) <= 1 || utf8.RuneCountInString(name) > 10 {
		return c.JSON(http.StatusOK, responses.Response(code.ParameterError, "角色不能小于2大于11个字符", ""))
	}


	roleManager, err := repositories.NewRoleManager()
	if err != nil {
		c.JSON(http.StatusOK, responses.Response(code.QueryError, "查询有误", err.Error()))
	}
	err = roleManager.UpdateRoleById(id, name)
	if err != nil {
		return c.JSON(http.StatusOK, responses.Response(code.QueryError, "sql错误", err.Error()))
	}

	return c.JSON(http.StatusOK, responses.Response(200, "成功", ""))
}

func DeleteRole(c echo.Context) error {

	var (
		getId       string
		id 			int64
		err			error
	)
	getId = c.Param("id")
	if getId == "" {
		return c.JSON(http.StatusOK, responses.Response(code.ParameterError,"id不能为空", ""))
	}

	id, err = strconv.ParseInt(getId, 10, 64)
	if err != nil {
		return c.JSON(http.StatusOK, responses.Response(code.ParameterError,err.Error(), ""))
	}


	roleManager, err := repositories.NewRoleManager()
	if err != nil {
		c.JSON(http.StatusOK, responses.Response(code.QueryError, "查询有误", err.Error()))
	}
	_, err = roleManager.DeleteRoleById(id)
	if err != nil {
		return c.JSON(http.StatusOK, responses.Response(code.QueryError, "sql错误", err.Error()))
	}

	return c.JSON(http.StatusOK, responses.Response(200, "成功", ""))
}

func GetRoles(c echo.Context) error {
	var (
		page	  int64
		size  	  int64
		name      string
		where     string
		sort	  string
		filed     string
	)

	r := new(request.Role)
	if err := c.Bind(r); err != nil {
		return c.JSON(http.StatusOK, responses.Response(code.ParameterError, "绑定参数失败", err))
	}
	page = r.Page
	size = r.Size
	name = r.Name
	sort = r.Sort
	filed = r.SortFile

	if page == 0 {
		page = 1
	}
	if size == 0 {
		size = 10
	}
	if !validate.SortValidate(sort) {
		sort = fileds.ASC
	}
	if !validate.SortFileValidate(filed, &models.Role{}) {
		filed = fileds.ID
	}
	where = " status = 1 "
	if name != "" {
		where += " AND name LIKE '%" +name+ "%'"
	}

	roleManager, err := repositories.NewRoleManager()
	if err != nil {
		c.JSON(http.StatusOK, responses.Response(code.QueryError, "查询有误", err.Error()))
	}
	resultList , err :=  roleManager.SelectRoles(where, page, size, sort, filed)
	if err != nil {
		return c.JSON(http.StatusOK, responses.Response(code.QueryError, "sql错误", err.Error()))
	}

	return c.JSON(http.StatusOK, responses.Response(200, "成功", resultList))
}
